import request from '@/utils/request'
import type {
  LoginParams,
  LoginResponse,
  UserInfoResponse,
  UserListParams,
  UserListResponse,
  CreateUserParams,
  CreateUserResponse,
  UpdateUserParams,
  UpdateUserResponse,
  ChangePasswordParams,
  ChangePasswordResponse,
  UserStatsResponse,
  LogoutResponse,
  DeleteUserResponse
} from './type'

// ===========API DEFINE START==================
const LOGIN = '/user/login'
const USERINFO = '/user/info'
const LOGOUT = '/user/logout'
const USER_LIST = '/user/list'
const USER_CREATE = '/user/create'
const USER_UPDATE = '/user'
const USER_DELETE = '/user'
const CHANGE_PASSWORD = '/user/change-password'
const USER_STATS = '/user/stats'
// ===========API DEFINE END====================

// 用户认证相关
const login = (params: LoginParams): Promise<LoginResponse> => {
  return request.post(LOGIN, params)
}

const logout = (): Promise<LogoutResponse> => {
  return request.post(LOGOUT)
}

const getUserInfo = (): Promise<UserInfoResponse> => {
  return request.get(USERINFO)
}

// 用户管理相关
const getUserList = (params?: UserListParams): Promise<UserListResponse> => {
  return request.get(USER_LIST, { params })
}

const createUser = (params: CreateUserParams): Promise<CreateUserResponse> => {
  return request.post(USER_CREATE, params)
}

const updateUser = (
  id: number,
  params: UpdateUserParams
): Promise<UpdateUserResponse> => {
  return request.put(`${USER_UPDATE}/${id}`, params)
}

const deleteUser = (id: number): Promise<DeleteUserResponse> => {
  return request.delete(`${USER_DELETE}/${id}`)
}

const changePassword = (
  params: ChangePasswordParams
): Promise<ChangePasswordResponse> => {
  return request.put(CHANGE_PASSWORD, params)
}

const getUserStats = (): Promise<UserStatsResponse> => {
  return request.get(USER_STATS)
}

export {
  login,
  logout,
  getUserInfo,
  getUserList,
  createUser,
  updateUser,
  deleteUser,
  changePassword,
  getUserStats
}
